package lesson.teacher;

import lesson.db.DbUtil;
import lesson.until.Md5Until;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class TeacherService {



    /**
     * 保存年级
     * @param teacher
     */
    public String saveTeacher(Teacher teacher) {

        if (teacher.getName() == null || teacher.getName().equals("")) {
            return "教师姓名不可为空";
        }
        if (teacher.getPhone() == null || teacher.getPhone().equals("")) {
            return "教师手机号不可为空";
        }
        if ((teacher.getId() == null || teacher.getId() == 0) &&  (teacher.getPassword() == null || teacher.getPassword().equals(""))) {
            return "教师密码不可为空";
        }

        if (checkExist(teacher)) {
            return "该手机号已存在";
        }


        if (teacher.getId() == null || teacher.getId() == 0) {
            teacher.setPassword(Md5Until.cal(teacher.getPassword()));
            SqlSession session = DbUtil.getSession(true);
            try{
                TeacherDao teacherDao = session.getMapper(TeacherDao.class);
                teacherDao.addTeacher(teacher);
            }catch (Exception e){
                e.printStackTrace();
            }finally {

                session.close();
            }


        } else {
            SqlSession session = DbUtil.getSession(true);
            TeacherDao teacherDao = session.getMapper(TeacherDao.class);
            teacherDao.updateTeacher(teacher);
            session.close();
        }

        return null;

    }


    /**
     * 检测叫这个名字的用户是否存在
     * @param teacher
     * @return
     */
    public boolean checkExist(Teacher teacher) {
        SqlSession session = DbUtil.getSession(true);
        try{
            TeacherDao teacherDao = session.getMapper(TeacherDao.class);
            boolean exist = teacherDao.checkExist(teacher);
            return exist;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }


        return false;
    }

    /**
     * 根据关键字查询
     * @param keywords
     * @return
     */
    public List<Teacher> list(String keywords) {
        SqlSession session = DbUtil.getSession(true);
        try{
            TeacherDao teacherDao = session.getMapper(TeacherDao.class);
            List<Teacher> list = teacherDao.list(keywords);
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }finally {

            session.close();
        }

        return null;
    }


    public String del(Integer id) {
        SqlSession session = DbUtil.getSession(true);
        try{
            TeacherDao teacherDao = session.getMapper(TeacherDao.class);
            teacherDao.delTeacher(id);


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();

        }

        return null;
    }

    public Teacher getById(Integer id) {
        SqlSession session = DbUtil.getSession(true);

        try{
            TeacherDao teacherDao = session.getMapper(TeacherDao.class);
            Teacher teacher = teacherDao.getById(id);
            return teacher;
        }catch (Exception e){
            e.printStackTrace();
        }finally {

            session.close();
        }

      return null;
    }
}
